package com.dzu.easy;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author ZhaoDong
 * @date 2022/12/29 9:59
 * @description 20. 有效的括号
 */
public class Test20 {
    public static void main(String[] args) {
        System.out.println(isValid("()[]{}"));
        System.out.println(isValid("([)]"));

    }
    public static boolean isValid(String s) {

        Deque<String> stack = new ArrayDeque<>();
        for (String str : s.split("")) {
            if (str.equals("(")) {
                stack.addLast(")");
            } else if (str.equals("[")) {
                stack.addLast("]");
            } else if (str.equals("{")) {
                stack.addLast("}");
            } else if (stack.isEmpty() || !stack.removeLast().equals(str)) {
                return false;
            }
        }
        return stack.isEmpty();

    }
}
